package com.lik888.entity;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
public class UserLogin  implements UserDetails {
    private User user;

    private List<String> list;

    public UserLogin(User user, List<String> list) {
        this.user = user;
        this.list = list;
    }

    @JsonIgnore
    public List<SimpleGrantedAuthority> authorities;


    // 重写getAuthorities方法，返回用户的权限列表
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(authorities != null) {
            return authorities;
        }

        authorities = new ArrayList<>();
        for (String item : list) {
            authorities.add(new SimpleGrantedAuthority(item));
        }
        return authorities;
    }

    // 重写getPassword方法，返回用户的密码
    @Override
    public String getPassword() {

        String password = user.getPassword();
        System.out.println("数据库密码：" + password);
        System.out.println("密码长度：" + password.length());
        return password;
    }


    // 重写getUsername方法，返回用户的用户名Q
    @Override
    public String getUsername() {
        return user.getUsername();
    }



    // 重写isAccountNonExpired方法，返回用户账号是否过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    // 重写isAccountNonLocked方法，返回用户账号是否被锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    // 重写isCredentialsNonExpired方法，返回用户凭证是否过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }


    // 重写isEnabled方法，返回用户是否启用
    @Override
    public boolean isEnabled() {
        return true;
    }
}
